package model.base;
import java.util.HashMap;

import model.algorithm.SearchDomain;
import model.domain.MazeDomain;
import model.domain.Puzzle8Domain;

public class DomainFactory {
	private HashMap<String, DomainCreator> domains;	

	public DomainFactory() {									// C'tor 
		domains = new HashMap<String, DomainCreator>();			// defines cases for  domain creators according to the string given
		domains.put("8Puzzle", new Puzzle8DomainCreator());
		domains.put("Maze", new MazeDomainCreator());
	}
		
	public SearchDomain createDomain(String domainNamePlusStartingState){		// will be operated by selectDomain in MyModel which will be operated by UserCommands
		String startingState = null;
		String[] stringArr = domainNamePlusStartingState.split(":");			// will get a String and split it to 2 strings - before and after ':'
		if (stringArr.length>1){
			startingState = stringArr[1];
		}
																	
		DomainCreator creator = domains.get(stringArr[0]);					//	creates a creator according to the first part of the string devided before 
		SearchDomain domain = null;
		if (creator != null)  {
			domain = creator.create(startingState);							// and now the creator creates the right domain
		}
		return domain;	
	}
	
	
////////////////////////////////////////////////////////////////////////
	
	private interface DomainCreator{									// DomainCreator Interface
		SearchDomain create(String StatringState);
	}
	
	private class Puzzle8DomainCreator implements DomainCreator{		// sort of a DomainCreator - specific for puzzle8Domain
		@Override														
		public SearchDomain create(String StatringState) {				// creates an instance of puzzle8Domain
			return new Puzzle8Domain(StatringState);
		}
	}
	
	private class MazeDomainCreator implements DomainCreator{			// sort of a DomainCreator - specific for MazeDomain
		@Override
		public SearchDomain create(String StatringState) {					
			return new MazeDomain(StatringState);						// creates an instance of MazeDomain
		}
	}
}
